<template>
    <el-form :model="ruleForm" :rules="rules" ref="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="老师名称" prop="name">
            <el-input placeholder="请输入老师名称" v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="老师头像" >
            <!--<el-upload class="upload-demo" drag action="https://jsonplaceholder.typicode.com/posts/" multiple>-->
                <!--<i class="el-icon-upload"></i>-->
                <!--<div class="el-upload__text">点击上传老师的图片</div>-->
            <!--</el-upload>-->
            <el-upload
                    class="avatar-uploader"
                    :action="actionImageUrl"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>
        <el-form-item label="老师简介" prop="content">
            <el-input type="textarea" placeholder="请输入简介，控制在300字内" v-model="ruleForm.content"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="sureAddTeacher">保存</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    import { ImageAction, ApiAddTeacherList } from '../../common/js/api'
    export default {
        data() {
            return {
                actionImageUrl: ImageAction + 'admin/image/uploadImage',
                imageUrl: '',
                loadSureBtn: false,
                inputVisible: false,
                inputValue: '',
                ruleForm: {
                    name: '',
                    content: '',
                },
                rules: {
                    name: [
                        { required: true, message: '请输入老师名称', trigger: 'blur' },
                        // { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
                    ],
                    content: [
                        { required: true, message: '请输入老师简介', trigger: 'blur' }
                    ],


                }
            };
        },
        methods: {
            // 老师新增
            sureAddTeacher: function () {
                this.$refs['rules'].validate((valid) => {
                    if (valid) {
                        if(this.loadSureBtn){
                            return false
                        }
                        this.loadSureBtn = true;
                        let params = { // 老师新增
                            name: this.ruleForm.name,
                            image_url: this.imageUrl,
                            content: this.ruleForm.content
                        }
                        ApiAddTeacherList(params).then(res => {
                            if(res.data.data === true){
                                this.$message({
                                    message: "新增成功",
                                    type: 'success'
                                });
                            }
                        }).catch(() => {

                        });
                    }
                })
            },
            // 上传老师头像
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
            },
            beforeAvatarUpload() {
                // const isJPG = file.type === 'image/jpeg';
                // const isLt2M = file.size / 1024 / 1024 < 2;
                //
                // if (!isJPG) {
                //     this.$message.error('上传头像图片只能是 JPG 格式!');
                // }
                // if (!isLt2M) {
                //     this.$message.error('上传头像图片大小不能超过 2MB!');
                // }
                // return isJPG && isLt2M;
            }
        }
    }
</script>
<style lang="scss">
    .demo-ruleForm{
        width:600px;
        max-width: 100%;
    }
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>
